package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuBuzhou;
import cn.hellochaos.generator.entity.CaipuSummary;
import cn.hellochaos.generator.mapper.CaipuBuzhouMapper;
import cn.hellochaos.generator.service.CaipuBuzhouService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.List;

/**
* <p>
* 做法步骤 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class CaipuBuzhouServiceImpl extends ServiceImpl<CaipuBuzhouMapper, CaipuBuzhou> implements CaipuBuzhouService {

    @Autowired
    CaipuBuzhouMapper caipuBuzhouMapper;

    @Override
    public List<CaipuBuzhou> getCaipuBuzhouByCookBookId(int cookbookId) {

        QueryWrapper<CaipuBuzhou> queryWrapper = new QueryWrapper<CaipuBuzhou>();
        queryWrapper.eq("id_cookbook",cookbookId);
        List<CaipuBuzhou> caipuLists = caipuBuzhouMapper.selectList(queryWrapper);
        return caipuLists;
    }

    @Override
    public Page<CaipuBuzhou> listCaipuBuzhousByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuBuzhou: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<CaipuBuzhou> queryWrapper =  new QueryWrapper<CaipuBuzhou>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuBuzhou> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuBuzhou完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public CaipuBuzhou getCaipuBuzhouById(int id) {
        log.info("正在查询caipuBuzhou中id为{}的数据",id);
        CaipuBuzhou caipuBuzhou = super.getById(id);
        log.info("查询id为{}的caipuBuzhou{}",id,(null == caipuBuzhou?"无结果":"成功"));
        return caipuBuzhou;
    }

    @Override
    public int insertCaipuBuzhou(CaipuBuzhou caipuBuzhou) {
        log.info("正在插入caipuBuzhou");
        if (super.save(caipuBuzhou)) {
            log.info("插入caipuBuzhou成功,id为{}",caipuBuzhou.getCaipuBuzhouId());
            return caipuBuzhou.getCaipuBuzhouId();
        } else {
            log.error("插入caipuBuzhou失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuBuzhouById(int id) {
        log.info("正在删除id为{}的caipuBuzhou",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuBuzhou成功",id);
            return id;
        } else {
            log.error("删除id为{}的caipuBuzhou失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuBuzhou(CaipuBuzhou caipuBuzhou) {
        log.info("正在更新id为{}的caipuBuzhou",caipuBuzhou.getCaipuBuzhouId());
        if (super.updateById(caipuBuzhou)) {
            log.info("更新d为{}的caipuBuzhou成功",caipuBuzhou.getCaipuBuzhouId());
            return caipuBuzhou.getCaipuBuzhouId();
        } else {
            log.error("更新id为{}的caipuBuzhou失败",caipuBuzhou.getCaipuBuzhouId());
            throw new BizException("更新失败[id=" + caipuBuzhou.getCaipuBuzhouId() + "]");
        }
    }

}
